/**
 * 0018
 *
 * @Author : Ruoyu Wang
 * @DateTime : 2024/8/12 23:11
 */

#include <iostream>
#include <vector>
#include "algorithm"

using namespace std;

class Solution {
public:
    vector<vector<int>> fourSum(vector<int> &nums, int target) {
        if (nums.size() < 4) return {};
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        for (int i = 0; i < nums.size() - 3; ++i) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < nums.size() - 2; ++j) {
                if (j > i + 1 && nums[j] == nums[j - 1])continue;
                int l = j + 1;
                int r = nums.size() - 1;
                while (l < r) {
                    long long tmp = (long long) nums[i] + nums[j] + nums[l] + nums[r];
                    if (tmp > target) {
                        r--;
                    } else if (tmp < target) {
                        l++;
                    } else {
                        res.push_back({nums[i], nums[j], nums[l], nums[r]});
                        l++;
                        r--;
                        while (l < r && l > j + 1 && nums[l] == nums[l - 1])l++;
                        while (l < r && r < nums.size() - 1 && nums[r] == nums[r + 1])r--;
                    }
                }
            }
        }
        return res;
    }
};

int main() {

    return 0;
}